import { ElMessageBox, ElMessage } from 'element-plus';

const ElMessageBoxPromise = (text = '', type = '', title = '注意', dangerouslyUseHTMLString = false) => {
    return new Promise((resolve, reject) => {
        ElMessageBox.confirm(text, title, {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type,
            dangerouslyUseHTMLString
        })
            .then(() => {
                resolve(true);
            })
            .catch(() => {
                reject(false);
            });
    });
};

let messageDom = null;
const ResetMessage = options => {
    if (messageDom) messageDom.close();
    messageDom = ElMessage(options);
};
const typeArr = ['success', 'error', 'warning', 'info'];
typeArr.forEach(type => {
    ResetMessage[type] = options => {
        if (typeof options === 'string') options = { message: options };
        options.type = type;
        return ResetMessage(options);
    };
});

export default {
    ElMessageBoxPromise,
    ResetMessage
};
